// 腾讯云对象存储
import COS from 'cos-js-sdk-v5'
import { Message } from 'element-ui'
import { media_sign } from '@/api/material'

// #腾讯云cos秘钥
// COSV5_APP_ID=1257305643
// ## cos5的配置SECRET_ID
// COSV5_SECRET_ID=
// ##cos5的配置SECRET_key
// COSV5_SECRET_KEY=3
// COSV5_TIMEOUT=60
// COSV5_CONNECT_TIMEOUT=60
// COSV5_BUCKET=edu-1257305643
// COSV5_REGION=ap-guangzhou
// COSV5_CDN=https://edu-1257305643.cos.ap-guangzhou.myqcloud.com
// COSV5_SCHEME=https
// COSV5_READ_FROM_CDN=false

var config = {
  AppId: '1257305643',
  Bucket: 'edu-1257305643',
  Region: 'ap-guangzhou',
  Baseurl: 'https://edu-1257305643.cos.ap-guangzhou.myqcloud.com',
  key: ''
}

const init = new COS({
  AppId: config.AppId,
  Bucket: config.Bucket,
  Region: config.Region,
  Baseurl: config.Baseurl,
  getAuthorization: async function(options, callback) {
    const { code, data } = await media_sign()
    const { expiration, expiredTime, requestId, startTime, credentials } = data
    if (code) {
      Message.error('获取COS上传凭证失败！')
      return console.error('authorization invalid')
    }
    callback({
      TmpSecretId: credentials.tmpSecretId,
      TmpSecretKey: credentials.tmpSecretKey,
      XCosSecurityToken: credentials.sessionToken,
      StartTime: startTime,
      ExpiredTime: expiredTime,
      expiration: expiration,
      requestId: requestId
    })
  }
})

export default { init, config }
